#Andrew Gooch
#April 11, 2017
#Ripping Yarn, Political Communications
#Replication R file 7: Figure 4 favorability plot

#This R file creates Figure 5 using ggplot. This is the last R file for the main text results. 

#REQUIREMENTS:
#Must run May_weights.R before running code below

#AVERAGE TREATMENT EFFECTS# REFERENCED IN TEXT
story_sub<- subset(May15.svy.rake, AnonStoryDummy==1)
cont_sub<- subset(May15.svy.rake, ControlDummy==1)
cue_sub<- subset(May15.svy.rake, CueDummy==1)

svymean(~BidenFavor,design=cont_sub, na.rm=TRUE)
confint(svymean(~BidenFavor,design=cont_sub, na.rm=TRUE))

svymean(~BidenFavor,design=cue_sub, na.rm=TRUE)
confint(svymean(~BidenFavor,design=cue_sub, na.rm=TRUE))

svymean(~BidenFavor,design=story_sub, na.rm=TRUE)
confint(svymean(~BidenFavor,design=story_sub, na.rm=TRUE))





# by party ID 


##########################################
# mean support and confidence intervals MARCH
##########################################

#Personalized Story treatment

#Dems
storysub_Dem <- subset(May15.svy.rake, AnonStoryDummy==1 & PID3_lean_new==1)

Story_mean_May15_Dem <- svymean(~BidenFavor,design=storysub_Dem, na.rm=TRUE)[1]
Story_lower_May15_Dem <- confint(svymean(~BidenFavor,design=storysub_Dem, na.rm=TRUE))[1]
Story_upper_May15_Dem <- confint(svymean(~BidenFavor,design=storysub_Dem, na.rm=TRUE))[2]

#Reps
storysub_Rep <- subset(May15.svy.rake, AnonStoryDummy==1 & PID3_lean_new==-1)

Story_mean_May15_Rep <- svymean(~BidenFavor,design=storysub_Rep, na.rm=TRUE)[1]
Story_lower_May15_Rep <- confint(svymean(~BidenFavor,design=storysub_Rep, na.rm=TRUE))[1]
Story_upper_May15_Rep <- confint(svymean(~BidenFavor,design=storysub_Rep, na.rm=TRUE))[2]

#Inds
storysub_Ind <- subset(May15.svy.rake, AnonStoryDummy==1 & PID3_lean_new==0)

Story_mean_May15_Ind <- svymean(~BidenFavor,design=storysub_Ind, na.rm=TRUE)[1]
Story_lower_May15_Ind <- confint(svymean(~BidenFavor,design=storysub_Ind, na.rm=TRUE))[1]
Story_upper_May15_Ind <- confint(svymean(~BidenFavor,design=storysub_Ind, na.rm=TRUE))[2]

#Control Group

#Dems
contrsub_Dem <- subset(May15.svy.rake, ControlDummy==1 & PID3_lean_new==1)

contr_mean_May15_Dem <- svymean(~BidenFavor,design=contrsub_Dem, na.rm=TRUE)[1]
contr_lower_May15_Dem <- confint(svymean(~BidenFavor,design=contrsub_Dem, na.rm=TRUE))[1]
contr_upper_May15_Dem <- confint(svymean(~BidenFavor,design=contrsub_Dem, na.rm=TRUE))[2]

#Reps
contrsub_Rep <- subset(May15.svy.rake, ControlDummy==1 & PID3_lean_new==-1)

contr_mean_May15_Rep <- svymean(~BidenFavor,design=contrsub_Rep, na.rm=TRUE)[1]
contr_lower_May15_Rep <- confint(svymean(~BidenFavor,design=contrsub_Rep, na.rm=TRUE))[1]
contr_upper_May15_Rep <- confint(svymean(~BidenFavor,design=contrsub_Rep, na.rm=TRUE))[2]

#Inds
contrsub_Ind <- subset(May15.svy.rake, ControlDummy==1 & PID3_lean_new==0)

contr_mean_May15_Ind <- svymean(~BidenFavor,design=contrsub_Ind, na.rm=TRUE)[1]
contr_lower_May15_Ind <- confint(svymean(~BidenFavor,design=contrsub_Ind, na.rm=TRUE))[1]
contr_upper_May15_Ind <- confint(svymean(~BidenFavor,design=contrsub_Ind, na.rm=TRUE))[2]


#Cue Group

#Dems
cuesub_Dem <- subset(May15.svy.rake, CueDummy==1 & PID3_lean_new==1)

cue_mean_May15_Dem <- svymean(~BidenFavor,design=cuesub_Dem, na.rm=TRUE)[1]
cue_lower_May15_Dem <- confint(svymean(~BidenFavor,design=cuesub_Dem, na.rm=TRUE))[1]
cue_upper_May15_Dem <- confint(svymean(~BidenFavor,design=cuesub_Dem, na.rm=TRUE))[2]

#Reps
cuesub_Rep <- subset(May15.svy.rake, CueDummy==1 & PID3_lean_new==-1)

cue_mean_May15_Rep <- svymean(~BidenFavor,design=cuesub_Rep, na.rm=TRUE)[1]
cue_lower_May15_Rep <- confint(svymean(~BidenFavor,design=cuesub_Rep, na.rm=TRUE))[1]
cue_upper_May15_Rep <- confint(svymean(~BidenFavor,design=cuesub_Rep, na.rm=TRUE))[2]

#Inds
cuesub_Ind <- subset(May15.svy.rake, CueDummy==1 & PID3_lean_new==0)

cue_mean_May15_Ind <- svymean(~BidenFavor,design=cuesub_Ind, na.rm=TRUE)[1]
cue_lower_May15_Ind <- confint(svymean(~BidenFavor,design=cuesub_Ind, na.rm=TRUE))[1]
cue_upper_May15_Ind <- confint(svymean(~BidenFavor,design=cuesub_Ind, na.rm=TRUE))[2]





##########################################
# 3-panel plot, one for each PID, MARCH
##########################################

#first panel plot - Democrats

coef <- c(contr_mean_May15_Dem,
          cue_mean_May15_Dem,
          Story_mean_May15_Dem)

lb <-c(contr_lower_May15_Dem,
       cue_lower_May15_Dem,
       Story_lower_May15_Dem)

ub <-c(contr_upper_May15_Dem,
       cue_upper_May15_Dem,
       Story_upper_May15_Dem)


method <-c("Control Group",
           "Cue Only Group",
           "Impersonalized Story Group")


specification <-c(1,2,3)


df <- data.frame(coef, method, specification,lb,ub)

p1 <- ggplot(df, aes(specification,coef, color=method)) +
  geom_point(aes(shape=method),size=5, position = position_dodge(width = 0.2)) + 
  scale_color_manual(name=" ",values=c("blue","blue", "blue")) + 
  scale_shape_manual(name=" ", values=c(17,19,12)) + 
  theme_bw() +
  scale_x_continuous(" ", breaks=1, labels=c(" ")) + 
  labs(title = "Democrats") +
  scale_y_continuous("Favorability of Vice President Joe Biden (0-1)", limits=c(0.2,.8)) +
  geom_errorbar(aes(ymin=lb,ymax=ub),width = 0, position = position_dodge(width = 0.2), size=2) + 
  #geom_text(aes(2, 28, label="Biden Cue"),size=5, color="black") +
  #geom_text(aes(1, -10, label="R"),size=6, color="black") +
  #geom_text(aes(2, -24, label="I"),size=6, color="black") +
  #geom_text(aes(3, -14.4, label="D"),size=6, color="black") +
  theme(axis.text.x=element_text(size=rel(1.3)),
        axis.title.x=element_text(size=rel(1.7)),
        axis.title.y=element_text(size=rel(1.3)),
        axis.text.y=element_text(size=rel(1.7)),
        panel.grid.minor=element_blank(),
        panel.grid.major.x=element_blank(),
        axis.ticks.y = element_blank(), 
        axis.ticks.x = element_blank(),
        panel.border = element_blank(),
        axis.line.x = element_line(color = 'black'),
        legend.key.size = unit(1., "cm"),
        legend.text=element_text(size=rel(1.7)), 
        legend.key = element_blank(),
        legend.title=element_blank(),
        plot.title = element_text(size = rel(1.7)),
        legend.direction = "horizontal", 
        #legend.justification=c(1,0), 
        legend.position=c(.50,.95)
  )

print(p1)

#second panel plot - Independents

coef <- c(contr_mean_May15_Ind,
          cue_mean_May15_Ind,
          Story_mean_May15_Ind)

lb <-c(contr_lower_May15_Ind,
       cue_lower_May15_Ind,
       Story_lower_May15_Ind)

ub <-c(contr_upper_May15_Ind,
       cue_upper_May15_Ind,
       Story_upper_May15_Ind)


method <-c("Control Group",
           "Cue Only Group",
           "Impersonalized Story Group")


specification <-c(1,2,3)



df <- data.frame(coef, method, specification,lb,ub)

p2 <- ggplot(df, aes(specification,coef, color=method)) +
  geom_point(aes(shape=method),size=5, position = position_dodge(width = 0.2)) + 
  scale_color_manual(name=" ",values=c("grey","grey", "grey")) + 
  scale_shape_manual(name=" ", values=c(17,19,12)) + 
  theme_bw() +
  scale_x_continuous(" ", breaks=1, labels=c(" ")) + 
  labs(title = "Independents") +
  scale_y_continuous(" ", limits=c(0.2,.8)) +
  geom_errorbar(aes(ymin=lb,ymax=ub),width = 0, position = position_dodge(width = 0.2), size=2) + 
  #geom_text(aes(2, 28, label="Biden Cue"),size=5, color="black") +
  #geom_text(aes(1, -10, label="R"),size=6, color="black") +
  #geom_text(aes(2, -24, label="I"),size=6, color="black") +
  #geom_text(aes(3, -14.4, label="D"),size=6, color="black") +
  theme(axis.text.x=element_text(size=rel(1.3)),
        axis.title.x=element_text(size=rel(1.7)),
        axis.title.y=element_text(size=rel(1.3)),
        axis.text.y=element_text(size=rel(1.7)),
        panel.grid.minor=element_blank(),
        panel.grid.major.x=element_blank(),
        axis.ticks.y = element_blank(), 
        axis.ticks.x = element_blank(),
        panel.border = element_blank(),
        axis.line.x = element_line(color = 'black'),
        legend.key.size = unit(1., "cm"),
        legend.text=element_text(size=rel(1.7)), 
        legend.key = element_blank(),
        legend.title=element_blank(),
        plot.title = element_text(size = rel(1.7)),
        legend.direction = "horizontal", 
        #legend.justification=c(1,0), 
        legend.position=c(.50,.95)
  )

print(p2)


#third panel plot - Republicans


coef <- c(contr_mean_May15_Rep,
          cue_mean_May15_Rep,
          Story_mean_May15_Rep)

lb <-c(contr_lower_May15_Rep,
       cue_lower_May15_Rep,
       Story_lower_May15_Rep)

ub <-c(contr_upper_May15_Rep,
       cue_upper_May15_Rep,
       Story_upper_May15_Rep)


method <-c("Control Group",
           "Cue Only Group",
           "Impersonalized Story Group")


specification <-c(1,2,3)


df <- data.frame(coef, method, specification,lb,ub)

p3 <- ggplot(df, aes(specification,coef, color=method)) +
  geom_point(aes(shape=method),size=5, position = position_dodge(width = 0.2)) + 
  scale_color_manual(name=" ",values=c("red","red", "red")) + 
  scale_shape_manual(name=" ", values=c(17,19,12)) + 
  theme_bw() +
  scale_x_continuous(" ", breaks=1, labels=c(" ")) + 
  labs(title = "Republicans") +
  scale_y_continuous(" ", limits=c(.2,.8)) +
  geom_errorbar(aes(ymin=lb,ymax=ub),width = 0, position = position_dodge(width = 0.2), size=2) + 
  #geom_text(aes(2, 28, label="Biden Cue"),size=5, color="black") +
  #geom_text(aes(1, -10, label="R"),size=6, color="black") +
  #geom_text(aes(2, -24, label="I"),size=6, color="black") +
  #geom_text(aes(3, -14.4, label="D"),size=6, color="black") +
  theme(axis.text.x=element_text(size=rel(1.3)),
        axis.title.x=element_text(size=rel(1.7)),
        axis.title.y=element_text(size=rel(1.3)),
        axis.text.y=element_text(size=rel(1.7)),
        panel.grid.minor=element_blank(),
        panel.grid.major.x=element_blank(),
        axis.ticks.y = element_blank(), 
        axis.ticks.x = element_blank(),
        panel.border = element_blank(),
        axis.line.x = element_line(color = 'black'),
        legend.key.size = unit(1., "cm"),
        legend.text=element_text(size=rel(1.7)), 
        legend.key = element_blank(),
        legend.title=element_blank(),
        plot.title = element_text(size = rel(1.7)),
        legend.direction = "horizontal", 
        #legend.justification=c(1,0), 
        legend.position=c(.50,.95)
  )

print(p3)

g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}

mylegend<-g_legend(p2)

p4 <- grid.arrange(arrangeGrob(p1 + theme(legend.position="none"),
                               p2 + theme(legend.position="none"),
                               p3 + theme(legend.position="none"),
                               nrow=1),
                   mylegend, nrow=2,heights=c(10, 1))




